<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单去重策略指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: linear-gradient(135deg, #1e1e2e 0%, #2d2d44 100%);
            border-radius: 12px;
            overflow: hidden;
        }
        .strategy-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }
        @keyframes float {
            0% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
            100% { transform: translateY(0px); }
        }
        .float-animation {
            animation: float 3s ease-in-out infinite;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
        }
        pre {
            margin: 0;
            padding: 1.5rem;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 0.875rem;
            line-height: 1.6;
            color: #e2e8f0;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-purple-600 via-indigo-600 to-blue-600 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24">
            <div class="max-w-4xl mx-auto text-center">
                <div class="float-animation inline-block mb-6">
                    <i class="fas fa-shield-alt text-6xl opacity-80"></i>
                </div>
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">订单去重策略指南</h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    构建高可靠性系统的核心实践，确保每个订单只被处理一次
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-check-circle mr-2"></i> 幂等性设计
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-database mr-2"></i> 数据库约束
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-sync-alt mr-2"></i> 防重机制
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="container mx-auto px-6 py-16">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap serif-font">处</span>理项目中的重复订单问题可以采用多种策略，主要目标是确保每个订单只被处理一次，避免因系统问题、网络波动或用户重复操作导致的订单重复。本指南将深入探讨六大核心策略，帮助您构建一个稳定可靠的订单处理系统。
                </p>
            </div>
        </div>
    </section>

    <!-- Strategy Overview Diagram -->
    <section class="container mx-auto px-6 py-8">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-8 gradient-text">订单去重策略全景图</h2>
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <div class="mermaid">
                    graph TD
                        A[订单去重策略] --> B[幂等性设计]
                        A --> C[数据库约束]
                        A --> D[消息队列去重]
                        A --> E[防重策略]
                        A --> F[事务处理]
                        A --> G[日志与监控]
                        
                        B --> B1[唯一标识符]
                        B --> B2[幂等接口]
                        
                        C --> C1[唯一索引]
                        C --> C2[乐观锁]
                        
                        D --> D1[消息去重]
                        D --> D2[幂等消息处理]
                        
                        E --> E1[前端防重]
                        E --> E2[防重校验]
                        
                        F --> F1[分布式事务]
                        F --> F2[事务隔离]
                        
                        G --> G1[日志记录]
                        G --> G2[系统监控]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                        style B fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff
                        style G fill:#8b5cf6,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>
    </section>

    <!-- Strategies Grid -->
    <section class="container mx-auto px-6 py-16">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-4xl font-bold text-center mb-12 serif-font">六大核心策略详解</h2>
            
            <!-- Strategy 1: 幂等性设计 -->
            <div class="mb-12">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden card-hover">
                    <div class="bg-gradient-to-r from-purple-500 to-indigo-600 p-6">
                        <h3 class="text-2xl font-bold text-white flex items-center">
                            <i class="fas fa-fingerprint mr-3"></i> 1. 幂等性设计
                        </h3>
                    </div>
                    <div class="p-8">
                        <p class="text-gray-700 mb-6 leading-relaxed">
                            确保订单操作是幂等的，即重复执行相同操作不会产生不同的结果。幂等性是分布式系统设计的核心原则之一。
                        </p>
                        
                        <div class="grid md:grid-cols-2 gap-6 mb-8">
                            <div class="strategy-card p-6 rounded-xl">
                                <h4 class="font-bold text-lg mb-3 flex items-center">
                                    <i class="fas fa-key text-purple-600 mr-2"></i> 唯一标识符
                                </h4>
                                <p class="text-gray-600">为每个订单生成唯一的标识符（如 UUID），在处理订单时检查是否已存在相同的标识符。</p>
                            </div>
                            <div class="strategy-card p-6 rounded-xl">
                                <h4 class="font-bold text-lg mb-3 flex items-center">
                                    <i class="fas fa-code text-purple-600 mr-2"></i> 幂等操作接口
                                </h4>
                                <p class="text-gray-600">设计接口时保证幂等性，通过请求ID或事务ID来避免重复提交。</p>
                            </div>
                        </div>

                        <div class="code-block">
                            <div class="bg-gray-800 px-4 py-2 flex items-center justify-between">
                                <span class="text-gray-400 text-sm">OrderService.java</span>
                                <i class="fas fa-copy text-gray-400 cursor-pointer hover:text-white"></i>
                            </div>
                            <pre><code>@Service
public class OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Transactional
    public void processOrder(Order order) {
        if (orderRepository.existsByOrderId(order.getOrderId())) {
            // 订单已存在，返回或记录日志
            return;
        }
        // 处理订单逻辑
        orderRepository.save(order);
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Strategy 2: 数据库唯一性约束 -->
            <div class="mb-12">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden card-hover">
                    <div class="bg-gradient-to-r from-indigo-500 to-blue-600 p-6">
                        <h3 class="text-2xl font-bold text-white flex items-center">
                            <i class="fas fa-database mr-3"></i> 2. 数据库唯一性约束
                        </h3>
                    </div>
                    <div class="p-8">
                        <p class="text-gray-700 mb-6 leading-relaxed">
                            在数据库层面实现唯一性约束，从根本上防止重复插入订单数据，这是最可靠的防重机制之一。
                        </p>
                        
                        <div class="grid md:grid-cols-2 gap-6 mb-8">
                            <div class="strategy-card p-6 rounded-xl">
                                <h4 class="font-bold text-lg mb-3 flex items-center">
                                    <i class="fas fa-lock text-indigo-600 mr-2"></i> 唯一索引
                                </h4>
                                <p class="text-gray-600">在数据库中为订单的唯一标识字段设置唯一索引，数据库会自动阻止重复插入。</p>
                            </div>
                            <div class="strategy-card p-6 rounded-xl">
                                <h4 class="font-bold text-lg mb-3 flex items-center">
                                    <i class="fas fa-sync text-indigo-600 mr-2"></i> 乐观锁
                                </h4>
                                <p class="text-gray-600">使用乐观锁机制，通过版本号等字段来控制并发访问，防止重复订单。</p>
                            </div>
                        </div>

                        <div class="code-block">
                            <div class="bg-gray-800 px-4 py-2 flex items-center justify-between">
                                <span class="text-gray-400 text-sm">orders.sql</span>
                                <i class="fas fa-copy text-gray-400 cursor-pointer hover:text-white"></i>
                            </div>
                            <pre><code>ALTER TABLE orders ADD UNIQUE (order_id);</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Strategy 3: 消息队列去重 -->
            <div class="mb-12">
                <div class="bg-white rounded-2xl shadow-lg overflow-hidden card-hover">
                    <div class="bg-gradient-to-r from-blue-500 to-cyan-600 p-6">
                        <h3 class="text-2xl font-bold text-white flex items-center">
                            <i class="fas fa-envelope mr-3"></i> 3. 消息队列去重
                        </h3>
                    </div>
                    <div class="p-8">
                        <p class="text-gray-700 mb-6 leading-relaxed">
                            如果订单处理涉及消息队列，需要特别注意消息的重复消费问题，确保消息处理的幂等性。
                        </p>
                        
                        <div class="grid md:grid-cols-2 gap-6 mb-8">
                            <div class="strategy-card p-6 rounded-xl">
                                <h4 class="font-bold text-lg mb-3 flex items-center">
                                    <i class="fas fa-filter text-blue-600 mr-2"></i> 消息去重
                                </h4>
                                